package shop.controller.front.user;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import shop.core.FrontBaseController;
import shop.core.Services;
import shop.core.common.bean.JSONResult;
import shop.core.plugins.sms.SMS;
import shop.core.plugins.sms.SMSSendCodeResult;
import shop.core.plugins.sms.SMSVerifyCodeResult;
import shop.services.manage.system.bean.User;
import shop.services.manage.system.impl.UserService;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * Created by wzl on 2017/9/21.
 */
@Controller
@RequestMapping("/front/user/")
public class UserController extends FrontBaseController<User> {
    @Resource
    private UserService userService;
    @Override
    public Services<User> getService() {
        return userService;
    }


    /**
     * 跳转到我的首页
     *
     * @return
     */
    @RequestMapping("toMyPage")
    public String toMyPage() {
        return "/front/account/accountList";
    }

    /**
     * 跳转到个人信息页面
     *
     * @return
     */
    @RequestMapping("toUserInfo")
    public String toUserInfo() {
        return "/front/account/accountInformation";
    }

    /**
     * 跳转到绑定手机页面
     *
     * @return
     */
    @RequestMapping("toBind")
    public String toBind() {
        return "/front/account/accountBindPhone";
    }

    /**
     * 跳转到通信录列表页面
     *
     * @return
     */
    @RequestMapping("toSubList")
    public String toSubList() {
        return "/front/account/mailList";
    }

    /**
     * 跳转到通信录详情页面
     *
     * @return
     */
    @RequestMapping("toSubDetail")
    public String toSubDetail() {
        return "/front/account/mailDetail";
    }

    /**
     * 跳转到关于我们页面
     *
     * @return
     */
    @RequestMapping("toIntroduce")
    public String toIntroduce() {
        return "/front/account/accountFind";
    }


    /**
     * 跳转到二维码扫描
     *
     * @return
     */
    @RequestMapping("toUserCode")
    public String toUserCode() {
        return "/front/account/qrCode";
    }

    /**
     * 查询登录用户
     * @param
     * @return
     */
    @RequestMapping(value = "selectUser", method = RequestMethod.GET)
    @ResponseBody
    public JSONResult selectUser(HttpServletRequest request) {
        User user = (User) request.getSession().getAttribute("userInfo");
        jsonResult = new JSONResult();
        jsonResult.setData(user);
        return jsonResult;
    }

    /**
     * 更新用户信息
     *
     * @return jsonResult
     * @throws Exception
     */
    @RequestMapping(value = "updateUser", method = RequestMethod.POST)
    @ResponseBody
    public JSONResult updateUser(HttpServletRequest request,HttpSession session,User user) throws Exception {
        User rs = (User) request.getSession().getAttribute("userInfo");
        user.setId(rs.getId());
        jsonResult = new JSONResult();
        userService.update(user);
        User rs2=userService.selectOne(user);
        session.setAttribute("userInfo", rs2);//更新缓存
        return jsonResult;
    }

    /**
     * 发送验证码（绑定手机）
     *
     * @throws Exception
     */
    @RequestMapping(value = "getSMSCode", method = RequestMethod.POST)
    @ResponseBody
    public JSONResult getSMSCode(HttpServletRequest request) throws Exception {
        JSONResult rs = new JSONResult();
        String mobile = request.getParameter("mobile");
        //根据手机号码查询用户信息，判断是否存在手机号码
        User user = new User();
        user.setPhone(mobile);
        List<User> userList = userService.selectList(user);

        if(userList.size()>0){
            rs.setSuccess(false);
            rs.setMessage("该手机已绑定其它账号，请重新填写手机号码！");
            return rs;
        }
        //发送短信
        SMS sms = new SMS();
        SMSSendCodeResult smsSendCodeResult= sms.sendCode(mobile, "5kDaOD-SQ8AbS959Lb9QRE", "86", null, null);
        if(smsSendCodeResult.getCode()==200) {
            rs.setSuccess(true);
            rs.setMessage("短信发送成功！");
            rs.setData(smsSendCodeResult);
            logger.debug("验证码返回信息"+rs);
            return rs;
        }else {
            rs.setSuccess(false);
            rs.setMessage("短信发送失败！");
            return rs;
        }
    }

    /**
     * 校验验证码
     *
     * @throws Exception
     */
    @RequestMapping(value = "checkCode", method = RequestMethod.POST)
    @ResponseBody
    public JSONResult checkCode(HttpServletRequest request) throws Exception {
        JSONResult jsonResult = new JSONResult();
        User user = (User) request.getSession().getAttribute("userInfo");
        // SmsId 短信验证码唯一标识   SmsContent 短信验证码内容
        String smsId = request.getParameter("SmsId");
        String smsContent = request.getParameter("SmsContent");
        String phone = request.getParameter("phone");
        SMS sms = new SMS();
        SMSVerifyCodeResult rs = sms.verifyCode(smsId, smsContent);//验证码验证方法
        if (rs.getSuccess() == null || !rs.getSuccess()) {
            jsonResult.setSuccess(false);
            jsonResult.setMessage("短信验证码错误！");
        } else {
            jsonResult.setSuccess(true);
            user.setPhone(phone);
            userService.update(user);
        }
        return jsonResult;
    }

    /**
     * 查询用户下级列表
     *
     * @return
     * @throws Exception
     */
    @RequestMapping("selectSubList")
    @ResponseBody
    public JSONResult selectSubList(HttpServletRequest request) throws Exception {
        User user = (User) request.getSession().getAttribute("userInfo");
        User rs=new User();
        rs.setPid(user.getId());
        List<User> rsList = userService.selectSubList(rs);
        jsonResult = new JSONResult();
        if(rsList.size()!=0){
            jsonResult.setData(rsList);
        }
        return jsonResult;
    }
}